package com.yaxin.web.controller.outsourcing;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.yaxin.common.annotation.Log;
import com.yaxin.common.core.domain.PageQuery;
import com.yaxin.common.core.domain.R;
import com.yaxin.common.core.page.TableDataInfo;
import com.yaxin.common.enums.BusinessType;
import com.yaxin.common.excel.ExcelResult;
import com.yaxin.common.utils.poi.ExcelUtil;
import com.yaxin.outsourcing.R.Result;
import com.yaxin.outsourcing.domain.dto.OutsourcingPersonDTO;
import com.yaxin.outsourcing.domain.po.OutsourcingPerson;
import com.yaxin.outsourcing.domain.vo.*;
import com.yaxin.outsourcing.listener.OutSourcingPersonImportListener;
import com.yaxin.outsourcing.service.IAccessApplicationService;
import com.yaxin.outsourcing.service.IOutsourcingPersonService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 外协人员信息表 前端控制器
 * </p>
 *
 * @author zcc
 * @since 2025-07-29
 */
@SaIgnore
@RestController
@RequestMapping("/api/out")
@RequiredArgsConstructor
public class OutsourcingPersonController {

    @Autowired
    private IOutsourcingPersonService outsourcingPersonService;
    @Autowired
    private IAccessApplicationService accessApplicationService;


    /**
     * 查询外协人员列表
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<OutsourcingPerson> list(OutsourcingPerson outsourcingPerson, PageQuery pageQuery) {
        return outsourcingPersonService.queryPersonList(outsourcingPerson,pageQuery);
    }

    /**
     * 根据id查询外协人员
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<OutsourcingPerson> getPerson(@PathVariable Long id) {
        return Result.success(outsourcingPersonService.getPersonDetailById(id));
    }

    /**
     * 获取部门树列表
     */
    @GetMapping("/appIdTree/{userName}")
    public R<List<Tree<Long>>> AppIdTree(@PathVariable String userName) {
        return R.ok(accessApplicationService.selectAppIdTreeList(userName));
    }

    /**
     * 新增外协人员
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public Result<Long> addOutsourcingPerson(@RequestBody OutsourcingPersonDTO dto) {
        return Result.success(outsourcingPersonService.addOutsourcingPerson(dto));
    }

    /**
     * 删除外协人员
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<Boolean> deleteOutsourcingPerson(@RequestBody List<Long> ids) {
        return Result.success(outsourcingPersonService.deletePerson(ids));
    }

    /**
     * 修改外协人员
     * @param id
     * @param person
     * @return
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateOutsourcingPerson(@PathVariable Long id, @RequestBody OutsourcingPerson person) {
        return Result.success(outsourcingPersonService.updateOutPerson(id, person));
    }

    /**
     * 状态检查
     * @return
     */
    @PostMapping("/statusCheck")
    public Result<Void> statusCheck() {
        outsourcingPersonService.statusCheck();
        return Result.success();
    }

    /**
     * 导出外协人员数据
     * @param response
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        List<OutsourcingPerson> personList = new LambdaQueryChainWrapper<>(outsourcingPersonService.getBaseMapper())
            .eq(OutsourcingPerson::getStatus, 0)
            .list();
        List<OutSourcingPersonExportVO> voList = new ArrayList<>();
        for (OutsourcingPerson person : personList) {
            String company = accessApplicationService.getById(person.getAppId()).getOutUnit();
            String area = accessApplicationService.getById(person.getAppId()).getArea();
            String appNo = accessApplicationService.getById(person.getAppId()).getAppNo();
            OutSourcingPersonExportVO vo = new OutSourcingPersonExportVO();
            BeanUtils.copyProperties(person,vo);
            vo.setCompanyName(company);
            vo.setWorkArea(area);
            vo.setAppNo(Long.parseLong(appNo));
            voList.add(vo);
        }
        ExcelUtil.exportExcel(voList, "外协人员数据", OutSourcingPersonExportVO.class, response);
    }


    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<OutSourcingPersonImportVO> result = ExcelUtil.importExcel(file.getInputStream(), OutSourcingPersonImportVO.class, new OutSourcingPersonImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", OutSourcingPersonImportVO.class, response);
    }

    /**
     * 设置ic卡号
     * @param person
     */
    @PostMapping("/setIcNo")
    public void setIcNoForPerson(OutsourcingPerson person){
        outsourcingPersonService.setIcNoForPerson(person);
    }

    /**
     * 为申请单所有人员设置IC卡号
     */
    @PostMapping("/setIcNoForAppId")
    public R<Void> setIcNoForAppId(@RequestParam Long appId){
        outsourcingPersonService.setIcNoForAppId(appId);
        return R.ok();
    }

    /**
     * 打印人员临时工作证
     */
    @PostMapping("/printPersonWorkCard")
    public R<Void> printPersonWorkCard(@RequestBody List<Long> ids){
        outsourcingPersonService.printPersonWorkCard(ids);
        return R.ok();
    }




}
